using System;

namespace org.paulbanks.BarcodeDotNet.Encoders
{
	/// <summary>
	/// Summary description for Code128_C.
	/// </summary>
	public class Code128_C : Code128 {

		public Code128_C(){	
		}

		override public bool Verify(string strText) {

			if (strText=="") return false; // Not empty
			if ((strText.Length % 2)!=0) return false; // Must be even number of digits

			char[] a = strText.ToCharArray();            
			for (int i=0;i<strText.Length;i++)
				if (!Char.IsDigit(a[i])) return false; // Must be all digits		

			return true;			

		}

		override public string Encode(string strText) 
		{
            
			if (!Verify(strText)) throw new Exception("Invalid");
            
			string strEnc = "0"; // First space
			strEnc += codeset[START_C];
			int checksum = 0;
			checksum += START_C;

			char[] a = strText.ToCharArray();
			for (int i=0;i<strText.Length;i+=2) 
			{
				int code = (a[i]-'0') * 10 + a[i+1] - '0';
				strEnc += codeset[code];
				checksum += code * (i/2+1);
			}

			// Add checksum and end marker
			checksum %= 103;
			strEnc += codeset[checksum];
			strEnc += codeset[STOP];

			return strEnc;
            
		}

	}
}
